package com.xch.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xch.mapper.UsersesMapper;
import com.xch.pojo.Userses;
import com.xch.pojo.UsersesExample;
import com.xch.vo.LoginVO;
import com.xch.vo.ResultVO;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Enumeration;
import java.util.List;

@Service
public class UserService {

    @Resource
    private UsersesMapper usersesMapper;

    public PageInfo<Userses> getUserList(Integer pageNumber, Integer pageSize, LoginVO vo){
        UsersesExample example = new UsersesExample();
        UsersesExample.Criteria criteria = example.createCriteria();

        if(vo!=null){
            if(vo.getUserName()!=null && ! "".equals(vo.getUserName().trim())){
                criteria.andUserNameLike("%"+vo.getUserName().trim()+"%");
            }
        }
        PageHelper.startPage(pageNumber,pageSize);
        List<Userses> userses = usersesMapper.selectByExample(example);
        PageInfo<Userses> usersesPageInfo = new PageInfo<>(userses);
        return usersesPageInfo;
    }

    public ResultVO<Userses> login(HttpSession session, LoginVO vo){
        UsersesExample example = new UsersesExample();
        UsersesExample.Criteria criteria = example.createCriteria();
        System.out.println(vo);
        if(vo!=null){
            if(vo.getUserName()!=null && !"".equals(vo.getUserName().trim())){
                criteria.andUserNameEqualTo(vo.getUserName());
            }else{
                return new ResultVO<>(500,"用户名为空");
            }
            if(vo.getPassword()!=null && !"".equals(vo.getPassword().trim())){
                criteria.andPasswordEqualTo(vo.getPassword());
            }else{
                return new ResultVO<>(500,"密码为空");
            }
        }
        List<Userses> userses = usersesMapper.selectByExample(example);
        if(userses.size() == 0){
            return new ResultVO<>(500,"用户名/密码不正确");
        }else{
            //登陆成功时将登录信息记录在session中
            Userses user = new Userses();
            user.setUserName(vo.getUserName());
            user.setPassword(vo.getPassword());
            user.setId(userses.get(0).getId());

            session.setAttribute("user",user);
            Userses user1 = (Userses) session.getAttribute("user");
            ResultVO<Userses> rs = new ResultVO<>(user1);
            return rs;
        }

    }

    public ResultVO<Boolean> logout(HttpSession session){
        // 清除session
        session.removeAttribute("user");
        Userses user = (Userses)session.getAttribute("user");
        return new ResultVO<Boolean>();
    }

    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public ResultVO<Boolean> revisePassword(HttpSession session,LoginVO vo){
        Userses user = (Userses)session.getAttribute("user");
        String password = vo.getPassword();
        if(!user.getPassword().equals(password)){
            System.out.println("原密码输入错误");
            return new ResultVO<>(500,"原密码输入错误");
        }
        Integer id = user.getId();
        Userses userses1 = usersesMapper.selectByPrimaryKey(id);
        userses1.setPassword(vo.getNewPassword());
        usersesMapper.updateByPrimaryKey(userses1);
        System.out.println("修改成功");
        return new ResultVO<>();
    }
}
